package cn.kgc.shiro.test;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @author: mosin
 * @version: v1.0  2022/5/30
 * 用户自定义realm对象
 */
public class CustomerRealm  extends AuthorizingRealm {

    //授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {





        return null;
    }

    //认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取用户的身份信息
        String principal = (String) token.getPrincipal();
        System.out.println("principal = " + principal);
        //  TODO 根据用户名 查询数据库  user
        if("admin".equals(principal)){
            //将根据用户名从数据库中查询出的用户对象进行封装  AuthenticationInfo   info
           return  new SimpleAuthenticationInfo(principal, "123", this.getName());
        }
        return null;
    }
}
